package org.ccay.manage.i18n.service.impl;

import java.io.Serializable;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.inject.Inject;
import javax.inject.Named;

import org.ccay.core.base.BatchVO;
import org.ccay.core.base.PageVO;
import org.ccay.core.base.PagedResult;
import org.ccay.core.exception.ApplicationException;
import org.ccay.core.log.CcayLoggerFactory;
import org.ccay.core.log.ILogger;
import org.ccay.core.request.Application;
import org.ccay.core.request.IApplicationPrincipal;
import org.ccay.core.util.DaoUtil;
import org.ccay.excel.exporter.IExcelDataProvider;
import org.ccay.excel.exporter.service.IExcelExportAssistant;
import org.ccay.i18n.I18nVO;
import org.ccay.i18n.service.ReloadableResourceBundle;
import org.ccay.manage.i18n.dao.II18nManageDao;
import org.ccay.manage.i18n.service.II18nManageService;
import org.ccay.security.util.SecurityUtil;

/**
 * 国际化资源提示信息 实现类
 * @since 2011-12-5
 */
@Named
public class I18nManageService implements II18nManageService,IExcelDataProvider {
	private static final String MESSAGE = "I18n";
	private static final ILogger logger = CcayLoggerFactory.getLogger(I18nManageService.class); 
	
	@Inject
	ReloadableResourceBundle bundle;
	
	@Inject
	private II18nManageDao i18nDao;
	
	@Inject
	private IExcelExportAssistant localExcelExportAssistant;
	
	public int createI18n(I18nVO newI18n) throws ApplicationException {
		int result =  DaoUtil.assertEffected(i18nDao.createI18n(newI18n), MESSAGE);
		bundle.reload();
		return result;
		
	}
	
	public void deleteI18n(I18nVO existedI18n) throws ApplicationException {
		DaoUtil.assertEffected(i18nDao.deleteI18n(existedI18n), MESSAGE);
		bundle.reload();
	}

	public void updateI18n(I18nVO existedI18n) throws ApplicationException {
		DaoUtil.assertEffected(i18nDao.updateI18n(existedI18n), MESSAGE);
		bundle.reload();
	}

	/**
	 * 批量删除
	 */
	public void deleteI18ns(List<I18nVO> existedI18ns) throws ApplicationException {
		DaoUtil.assertEffected(i18nDao.deleteI18ns(existedI18ns), existedI18ns.size(), MESSAGE);
		bundle.reload();
	}

	/**
	 * 批量更新
	 */
	public void updateI18ns(List<I18nVO> existedI18ns) throws ApplicationException {
		i18nDao.updateI18ns(existedI18ns);
		bundle.reload();
	}

	/**
	 * 批量更新
	 */
	public void createI18ns(List<I18nVO> newI18ns) throws ApplicationException {
		i18nDao.createI18ns(newI18ns);
		bundle.reload();
	}

	@Override
	public Map<String, Object> validateForBatch(BatchVO<I18nVO> batchItems) {
		Map<String, Object> errorInfo = new HashMap<String,Object>();
		if(!batchItems.getCreateAndUpdateUnion().isEmpty()){
			//校验提供的新增及修改的国际化是否重复
			Set<String> i18nEntry = new HashSet<String>();
			for(I18nVO row : batchItems.getCreateAndUpdateUnion()){
				String uniqueKey = row.getI18nLanguage()+"|"+row.getI18nKey();
				if(i18nEntry.contains(uniqueKey)){
					getListInMap(errorInfo,"LauguageI18nKeyUniqueFail",String.class).add(row.getI18nLanguage()+"+"+row.getI18nKey());
				}else{
					i18nEntry.add(uniqueKey);
				}
			}
			//与数据库中已存在的数据校验是否重复
			List<String> errors = i18nDao.checkLauguageI18nKeyUnique(batchItems);
			if(!errors.isEmpty()){
				getListInMap(errorInfo,"LauguageI18nKeyUniqueFail",String.class).addAll(errors);
			}
		}
		return errorInfo;
	}
	
	@SuppressWarnings("unchecked")
	private <T> Set<T> getListInMap(Map<String,Object> map, String type,Class<T> T){
		Set<T> set = (Set<T>)map.get(type);
		if(set == null){
			set = new HashSet<T>();
			map.put(type, set);
		}
		return set;
	}
	
	
	/**
	 * 批量操作i18n数据
	 */
	public void batchOperation(BatchVO<I18nVO> batchItems) throws ApplicationException {
		// 如果对象不为空 执行
		if (!batchItems.getItems2Delete().isEmpty()) {
			SecurityUtil.checkPermission("i18nManage:delete");
			deleteI18ns(batchItems.getItems2Delete());
		}
		// 如果对象不为空 执行
		if (!batchItems.getItems2Update().isEmpty()) {
			SecurityUtil.checkPermission("i18nManage:update");
			updateI18ns(batchItems.getItems2Update());
		}
		// 如果对象不为空 执行
		if (!batchItems.getItems2Create().isEmpty()) {
			SecurityUtil.checkPermission("i18nManage:create");
			createI18ns(batchItems.getItems2Create());
		}
	}
	
	
	
	//////////////以下为查询操作

	/* (non-Javadoc)
	 * @see org.ccay.i18n.service.II18nService#findRangeI18nList(org.ccay.i18n.I18nVO, org.ccay.core.base.PageVO, java.lang.String, int)
	 */
	//@CcayUrlBound(url = "ccay/i18n/listI18n.html")
	public PagedResult<I18nVO> findRangeI18nList(
			I18nVO queryI18n,
			PageVO pageVO) throws ApplicationException {
		return i18nDao.findRangeI18nList(queryI18n, pageVO);
	}
	
	//@CcayUrlBound(url = "ccay/i18n/listI18n.html")
	public PagedResult<I18nVO> findRangeI18nListFilter(
			I18nVO queryI18n,
			PageVO pageVO) throws ApplicationException {
		return i18nDao.findRangeI18nListFilter(queryI18n, pageVO);
	}


	public I18nVO findI18n(I18nVO queryI18n) throws ApplicationException {
		return DaoUtil.assertExists(i18nDao.findI18n(queryI18n), MESSAGE);
	}

	public List<I18nVO> findI18nList(I18nVO queryI18n) {
		logger.debug("Before findI18nList");
		return i18nDao.findI18nList(queryI18n);
	}

	public PagedResult<I18nVO> findPagedI18nList(I18nVO queryI18n, PageVO pageVO) {
		return i18nDao.findPagedI18nList(queryI18n, pageVO);
	}
	
	public PagedResult<I18nVO> findPagedI18nListFilter(I18nVO queryI18n, PageVO pageVO){
	   return i18nDao.findPagedI18nList(queryI18n, pageVO);
	}
	
	@Override
	public void exportI18n(I18nVO i18nVO, PageVO pageVO) throws ApplicationException {
		localExcelExportAssistant.submitExportTask("Manage.i18n", i18nVO, pageVO, null);
	}
	
	@Override
	public List<?> getBatchData(Serializable conditionObject, PageVO pageVO) throws ApplicationException {
		I18nVO i18n = (I18nVO)conditionObject;
		List<I18nVO> i18nList = null;
		PagedResult<I18nVO> resultList = i18nDao.findPagedI18nList(i18n, pageVO);
		i18nList = resultList.getResult();
		return i18nList;
	}

	@Override
	public void reloadAll() throws ApplicationException {
		//开发环境不校验权限
		if(Application.getCurrent().getEnviorment().equals(Application.DEV)){
			bundle.reload();
		}else{
			SecurityUtil.checkPermission("i18nManage:reload");
			bundle.reload();
		}
	}
}
